package procTest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import proc.BasicFileWrap;
import proc.FileWrap;

public class BasicFileWrapTest {

	private File report;
	private File result;
	private final String path = "src/procTest/IOFiles/";

	@Before
	public void setUp() {
		this.report = new File(this.path + "report_output");
		this.result = new File(this.path + "result_output");
		this.report.delete();
		this.result.delete();
	}

	private FileWrap fileWrapTest;

	/**
	 * The NO. of args is less than 4
	 */
	@Test
	public void priExistenceTest1() {
		String[] fileNames = { this.path + "primary_input",
				this.path + "auxiliary_input", this.path + "result_output" };
		try {
			this.fileWrapTest = new BasicFileWrap(fileNames);
			fail("No exception thrown!");
		} catch (IllegalArgumentException e) {
			// test passed
		}
	}

	/**
	 * The NO. of args is larger than 4
	 */
	@Test
	public void priExistenceTest2() {
		String[] fileNames = { this.path + "primary_input",
				this.path + "auxiliary_input", this.path + "result_output",
				this.path + "report_output", this.path + "extra_file" };
		try {
			this.fileWrapTest = new BasicFileWrap(fileNames);
			fail("No exception thrown!");
		} catch (IllegalArgumentException e) {
			// test passed
		}
	}

	/**
	 * The name of the primary input is misspelled
	 */
	@Test
	public void priExistenceTest3() throws IllegalArgumentException {
		String[] fileNames = { this.path + "wrong name",
				this.path + "auxiliary_input", this.path + "result_output",
				this.path + "report_output" };
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Error doesn't recognized.", false,
				this.fileWrapTest.checkFiles());
	}

	/**
	 * The primary input exists but No permission
	 */
	@Test
	public void priPmsTest() throws IllegalArgumentException {
		String[] fileNames = { this.path + "primary_input_no_pms",
				this.path + "auxiliary_input", this.path + "result_output",
				this.path + "report_output" };
		File primary_input = new File(path + "primary_input_no_pms");
		primary_input.setReadable(false);
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Error doesn't recognized.", false,
				this.fileWrapTest.checkFiles());
	}

	/**
	 * The primary file name is a directory
	 */
	@Test
	public void priDirTest() throws IllegalArgumentException {
		String[] fileNames = { this.path, this.path + "auxiliary_input",
				this.path + "result_output", this.path + "report_output" };
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Error doesn't recognized.", false,
				this.fileWrapTest.checkFiles());
	}

	/**
	 * The primary file is empty
	 */
	@Test
	public void priEptTest() {
		String[] fileNames = { this.path + "primary_input_empty",
				this.path + "auxiliary_input", this.path + "result_output",
				this.path + "report_output" };
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Large primary file is not able to get processed.", true,
				this.fileWrapTest.checkFiles());

	}

	/**
	 * The primary file is valid and extremely large
	 */
	@Test
	public void priExtTest() {
		String[] fileNames = { this.path + "primary_input_extreme",
				this.path + "auxiliary_input", this.path + "result_output",
				this.path + "report_output" };
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Large primary file is not able to get processed.", true,
				this.fileWrapTest.checkFiles());

	}

	/**
	 * The name of the aux input is misspelled
	 */
	@Test
	public void auxNameTest() throws IllegalArgumentException {
		String[] fileNames = { this.path + "primary_input",
				this.path + "wrong name", this.path + "result_output",
				this.path + "report_output" };
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Error doesn't recognized.", false,
				this.fileWrapTest.checkFiles());
	}

	/**
	 * The auxiliary input file exists but No permission
	 */
	@Test
	public void auxPmsTest() throws IllegalArgumentException {
		String[] fileNames = { this.path + "primary_input",
				this.path + "auxiliary_input_no_pms",
				this.path + "result_output", this.path + "report_output" };
		File aux_input = new File(path + "auxiliary_input_no_pms");
		aux_input.setReadable(false);
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Error is not recognized.", false,
				this.fileWrapTest.checkFiles());
	}

	/**
	 * The aux file name is a directory
	 * 
	 * @throws IOException
	 */
	@Test
	public void auxDirTest() throws IllegalArgumentException {
		String[] fileNames = { this.path + "primary_input", this.path,
				this.path + "result_output", this.path + "report_output" };
		this.fileWrapTest = new BasicFileWrap(fileNames);
		assertEquals("Error doesn't recognized.", false,
				this.fileWrapTest.checkFiles());
	}

}
